package com.centronix.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.centronix.bean.ComboBean;
import com.centronix.domain.Expense;
import com.centronix.domain.ExpenseType;
import com.centronix.manager.ExpenseManager;
import com.centronix.util.WebUtil;

@ManagedBean(name = "expenseBean")
@SessionScoped
public class ExpenseBean implements Serializable{

	private static final long serialVersionUID = 1L;

	@ManagedProperty("#{expenseManager}")
    private ExpenseManager expenseManager;
	
	private List<ComboBean> productTypeComboList =  new ArrayList<ComboBean>(0);
	private Date selectedDate = new Date();
	
	private String expenseTypeCode = "";
	private Expense newExpense = new Expense();
	private Expense selectedExpense = new Expense();
	
	private List<Expense> allExpense = new ArrayList<Expense>(0);
	
	private boolean dataVisible = false;
	private String selectedExpenseType = "";

	public String findAllExpenses(){
		allExpense = expenseManager.findAllExpense();
		productTypeComboList = expenseManager.getExpenseTypeCombo();
		dataVisible = true;
		return "expenses";
	}
	
	public String saveNewExpense(){
		int expType = Integer.valueOf(selectedExpenseType);
		ExpenseType expenseType = new ExpenseType();
		expenseType.setExpenseTypeId(expType);
		newExpense.setExpenseType(expenseType);
		expenseManager.saveExpense(newExpense);
		allExpense = expenseManager.findAllExpense();
		newExpense = new Expense();
		selectedExpenseType = "";
		WebUtil.showSuccessfulMessage("Record Saved!");
		return "";
	}
	
	public String updateExpense(){
		expenseManager.saveExpense(selectedExpense);
		allExpense = expenseManager.findAllExpense();
		selectedExpense = new Expense();
		selectedExpenseType = "";
		WebUtil.showSuccessfulMessage("Record Updated!");
		return "";
	}
	
	public String deleteExpense(){
		expenseManager.deleteExpense(selectedExpense.getExpenseId());
		allExpense = expenseManager.findAllExpense();
		selectedExpense = new Expense();
		selectedExpenseType = "";
		WebUtil.showSuccessfulMessage("Record Deleted!");
		return "";
	}
	
	public String filterExpense(){
		allExpense = expenseManager.filterExpense(selectedDate);
		return "";
	}

	public ExpenseManager getExpenseManager() {
		return expenseManager;
	}

	public void setExpenseManager(ExpenseManager expenseManager) {
		this.expenseManager = expenseManager;
	}

	public List<Expense> getAllExpense() {
		return allExpense;
	}

	public void setAllExpense(List<Expense> allExpense) {
		this.allExpense = allExpense;
	}

	public boolean isDataVisible() {
		return dataVisible;
	}

	public void setDataVisible(boolean dataVisible) {
		this.dataVisible = dataVisible;
	}

	public Date getSelectedDate() {
		return selectedDate;
	}

	public void setSelectedDate(Date selectedDate) {
		this.selectedDate = selectedDate;
	}

	public String getExpenseTypeCode() {
		return expenseTypeCode;
	}

	public void setExpenseTypeCode(String expenseTypeCode) {
		this.expenseTypeCode = expenseTypeCode;
	}

	public Expense getNewExpense() {
		return newExpense;
	}

	public void setNewExpense(Expense newExpense) {
		this.newExpense = newExpense;
	}

	public Expense getSelectedExpense() {
		return selectedExpense;
	}

	public void setSelectedExpense(Expense selectedExpense) {
		this.selectedExpense = selectedExpense;
	}

	public List<ComboBean> getProductTypeComboList() {
		return productTypeComboList;
	}

	public void setProductTypeComboList(List<ComboBean> productTypeComboList) {
		this.productTypeComboList = productTypeComboList;
	}

	public String getSelectedExpenseType() {
		return selectedExpenseType;
	}

	public void setSelectedExpenseType(String selectedExpenseType) {
		this.selectedExpenseType = selectedExpenseType;
	}


}
